package com.easyidea.its.web.servlet;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Text;
import org.dom4j.tree.DefaultText;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.easyidea.its.domain.model.system.Department;
import com.easyidea.its.domain.model.system.PassPort;
import com.easyidea.its.facade.user.UserMgr;
import com.easyidea.its.util.ItsConstants;
import com.easyidea.its.web.dto.SeesionUserDto;

public class TreeDataServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5752882187691476320L;
	private UserMgr userMgr;
	 @Override  
	    protected void doGet(HttpServletRequest req, HttpServletResponse resp)   
	            throws ServletException, IOException {   
	         this.doPost(req, resp);   
	    }   
	    @Override  
	    protected void doPost(HttpServletRequest req, HttpServletResponse resp)   
	             throws ServletException, IOException {    
	    	SeesionUserDto dto = (SeesionUserDto)req.getSession().getAttribute(ItsConstants.SESSION_KEY_USER_INFO);
	    	WebApplicationContext context =
	                WebApplicationContextUtils.getWebApplicationContext(
	                        this.getServletContext());
	    	userMgr = (UserMgr)context.getBean("userMgr");
	       
	        Document doc = null;
	        if(dto.getUserId() == 1){
	        	doc = createAdminXMLData(userMgr); 
	        }else {
	        	 dto = userMgr.getUserPassPortsByUserId(dto.getUserId());
	        	 doc = createXMLData(dto);  
	        }
	        String content = doc.asXML(); 
	        resp.setContentType("text/xml; charset=UTF-8"); 
	        resp.getWriter().write(content);   
	      }   
	    private static Document createAdminXMLData(UserMgr userMgr) throws IOException {   
	    	  Document document = DocumentHelper.createDocument();   
			  document.setXMLEncoding("UTF-8");   
			  
			  Element root = document.addElement("tree");   
			  root.addAttribute("id", "-1");  
				Element item_root = root.addElement("item");   
				item_root.addAttribute("text", "部门卡口信息");   
				item_root.addAttribute("id", "-2");  
				item_root.addAttribute("open", "1");  
				item_root.addAttribute("im0", "folderClosed.gif");   
				item_root.addAttribute("im1", "folderOpen.gif");   
				item_root.addAttribute("im2", "folderClosed.gif");  
				Element item_root_userdata = item_root.addElement("userdata");  
				item_root_userdata.addAttribute("name", "url");
			  Collection<Department> depts = userMgr.getAllDepartments();
			  int i = -3;
			  for(Department dept : depts) {
				if (dept != null && dept.getDepartmentName() != null 
						&& !dept.getDepartmentName().equals("")) {
					Element item = item_root.addElement("item");   
					item.addAttribute("text", dept.getDepartmentName());   
					item.addAttribute("id", String.valueOf(i--));  
					item.addAttribute("open", "1"); 
					item.addAttribute("im0", "folderClosed.gif");   
					item.addAttribute("im1", "folderOpen.gif");   
					item.addAttribute("im2", "folderClosed.gif"); 
					Element userdata = item.addElement("userdata");  
					userdata.addAttribute("name", "url");
					 Collection<PassPort> passPortList = userMgr.getAllPassPortByDeptId(dept.getId());
					for (PassPort passPort : passPortList) {
						 			
						  Element subItem = item.addElement("item");   
						  subItem.addAttribute("text", passPort.getPassPortName()); 
						  subItem.addAttribute("id",  passPort.getPassPortName());   	
						  subItem.addAttribute("im0", "folderClosed.gif");   
						  subItem.addAttribute("im1", "folderOpen.gif");   
						  subItem.addAttribute("im2", "folderClosed.gif"); 
						  Element subUserdata = subItem.addElement("userdata");  
						  subUserdata.addAttribute("name", "url");
						  Text text = new DefaultText("/its/auth/tracklogTabbar.action");			
						  subUserdata.add(text);
						  if(passPort.getStartpoint() != null && passPort.getEndpoint()!=null) {
							  Element startpoint = subItem.addElement("item");   
							  startpoint.addAttribute("text", passPort.getStartpoint()); 
							  startpoint.addAttribute("id",  passPort.getId().toString()+"_" + passPort.getStartpoint());   
							  startpoint.addAttribute("im0", "book_titel.gif");   
							  startpoint.addAttribute("im1", "fbook.gif");   
							  startpoint.addAttribute("im2", "book_titel.gif");  
							  Element startUserdata = startpoint.addElement("userdata");  
							  startUserdata.addAttribute("name", "url");	
							  Text text1 = new DefaultText("/its/auth/tracklogTabbar.action");			
							  startUserdata.add(text1);							  
							  Element endpoint = subItem.addElement("item");   
							  endpoint.addAttribute("text",  passPort.getEndpoint()); 
							  endpoint.addAttribute("id",  passPort.getId().toString()+"_" + passPort.getEndpoint());   
							  endpoint.addAttribute("im0", "book_titel.gif");   
							  endpoint.addAttribute("im1", "fbook.gif");   
							  endpoint.addAttribute("im2", "book_titel.gif"); 
							  Element endtUserdata = endpoint.addElement("userdata");  
							  endtUserdata.addAttribute("name", "url");	
							  Text text2 = new DefaultText("/its/auth/tracklogTabbar.action");			
							  endtUserdata.add(text2);								  
						  }
						 		  
					}
				}
				}
	    	  return document;   
	    
	   }   
	      private static Document createXMLData(SeesionUserDto dto) throws IOException {   
	    	  Document document = DocumentHelper.createDocument();   
			  document.setXMLEncoding("UTF-8");   
			  
			  Element root = document.addElement("tree");   
			  root.addAttribute("id", "-1");  
				Element item_root = root.addElement("item");   
				item_root.addAttribute("text", "部门卡口信息");   
				item_root.addAttribute("id", "-2");  
				item_root.addAttribute("open", "1");  
				item_root.addAttribute("im0", "folderClosed.gif");   
				item_root.addAttribute("im1", "folderOpen.gif");   
				item_root.addAttribute("im2", "folderClosed.gif");  
				Element item_root_userdata = item_root.addElement("userdata");  
				item_root_userdata.addAttribute("name", "url");
				if (dto != null && dto.getDepartmentName() != null 
						&& !dto.getDepartmentName().equals("")) {
					Element item = item_root.addElement("item");   
					item.addAttribute("text", dto.getDepartmentName());   
					item.addAttribute("id", "0");  
					item.addAttribute("open", "-3");  
					item.addAttribute("im0", "folderClosed.gif");   
					item.addAttribute("im1", "folderOpen.gif");   
					item.addAttribute("im2", "folderClosed.gif"); 
					Element userdata = item.addElement("userdata");  
					userdata.addAttribute("name", "url");
					 List<PassPort> passPortList = dto.getPassPortList();
					for (PassPort passPort : passPortList) {
						 			
						  Element subItem = item.addElement("item");   
						  subItem.addAttribute("text", passPort.getPassPortName()); 
						  subItem.addAttribute("id",  passPort.getPassPortName());   	
						  subItem.addAttribute("im0", "folderClosed.gif");   
						  subItem.addAttribute("im1", "folderOpen.gif");   
						  subItem.addAttribute("im2", "folderClosed.gif"); 
						  Element subUserdata = subItem.addElement("userdata");  
						  subUserdata.addAttribute("name", "url");
						  Text text = new DefaultText("/its/auth/tracklogTabbar.action");			
						  subUserdata.add(text);
						  if(passPort.getStartpoint() != null && passPort.getEndpoint()!=null) {
							  Element startpoint = subItem.addElement("item");   
							  startpoint.addAttribute("text", passPort.getStartpoint()); 
							  startpoint.addAttribute("id",  passPort.getId().toString()+"_" + passPort.getStartpoint());   
							  startpoint.addAttribute("im0", "book_titel.gif");   
							  startpoint.addAttribute("im1", "fbook.gif");   
							  startpoint.addAttribute("im2", "book_titel.gif");  
							  Element startUserdata = startpoint.addElement("userdata");  
							  startUserdata.addAttribute("name", "url");	
							  Text text1 = new DefaultText("/its/auth/tracklogTabbar.action");			
							  startUserdata.add(text1);							  
							  Element endpoint = subItem.addElement("item");   
							  endpoint.addAttribute("text",  passPort.getEndpoint()); 
							  endpoint.addAttribute("id",  passPort.getId().toString()+"_" + passPort.getEndpoint());   
							  endpoint.addAttribute("im0", "book_titel.gif");   
							  endpoint.addAttribute("im1", "fbook.gif");   
							  endpoint.addAttribute("im2", "book_titel.gif"); 
							  Element endtUserdata = endpoint.addElement("userdata");  
							  endtUserdata.addAttribute("name", "url");	
							  Text text2 = new DefaultText("/its/auth/tracklogTabbar.action");			
							  endtUserdata.add(text2);								  
						  }
						 		  
					}
				
				}
	    	  return document;   
	    
	   }   
	
}
